#pragma once
#include<iostream>
using namespace std;

template<class K>
struct BSTreeNode
{
	BSTreeNode <K>* _left;
	BSTreeNode <K>* _right;
	K _key;

	BSTreeNode(const K& key)
		:_left(nullptr)
		, _right(nullptr)
		, _key(key)
	{};
};

template<class K>
class BSTree
{
public:
	typedef BSTreeNode<K> node;
	bool insert(const K& key)
	{
		if (_root == nullptr)
		{
			_root = new node(key);
			return true;
		}
		node* parent = nullptr;
		node* cur = _root;
		while (cur != nullptr)
		{
			parent = cur;
			if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		
		if (parent->_key < key)
			parent->_right = new node(key);
		else
			parent->_left = new node(key);

		return true;
	}
	bool Find(const K& key)
	{
		node* cur = _root;
		while (cur != nullptr)
		{
			if (cur->_key < key)
			{
				cur = cur->_right;
			}
			else if (cur->_key > key)
			{
				cur = cur->_left;
			}
			else
			{
				return true;
			}
		}
		return false;
	}
	void _InOrder(node* root)
	{
		if (root == nullptr)
			return;
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}
	void InOrder()
	{
		_InOrder(_root);
	}
private:
	node* _root = nullptr;
};
